<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>如何创建 Atom</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="ShareURL0503.html">Share the URLs with u (Mar. 2005)</a>&nbsp;&nbsp;>>Next: <a href="050330.html">Class::DBI 改 Primary 项</a></p>
<h1>如何创建 Atom</h1>
<div class='content'>
<p>Category: <a href='Modules.html'>Modules</a> &nbsp; Keywords: <b>Atom</b></p><h2>简介</h2>
Atom 的作用请自行搜索。<br>
RSS 的创建请参阅<a href='RSS.html'>给网页增加RSS</a>.<p>

<h2>描述</h2>
这里我使用 <a href='http://search.cpan.org/perldoc?XML::Atom::SimpleFeed'>XML::Atom::SimpleFeed</a> 来创建 Atom<p>

XML::Atom::SimpleFeed 有些许瑕疵，如不能设置编码和多了个默认的 xml:lang="en"<p>
我略微修改了下使之符合我的要求。
<ol>
<li>删除两处 xml:lang="en"
<li>改 encoding="iso-8859-1" 为 encoding="UTF-8"
</ol>

<h2>代码</h2>
<p>如下代码来自我基于 Catalyst 的 Eplanet</p>
<pre><code>
use lib "E:/t/Eplanet/hackedlib"; # add the hack file's address
use XML::Atom::SimpleFeed;

my $Atom_file = $c->config->{build_root} . "/atom.xml";
my $title = 'Fayland\'s';
my $link = 'http://www.fayland.org/';
my $description = 'What Fayland says';
	
my $atom = XML::Atom::SimpleFeed->new(
	title    => $title,
	link     => $link,
	tagline  => $description,
	author => { name => 'Fayland Lam' },
)
or die;

my @cats = Eplanet::M::CDBI::Cms->retrieve_from_sql(qq{
	1=1 ORDER BY cms_id DESC LIMIT 0, 20
});

foreach my $cat (@cats) {
	my $_title = $cat->get('cms_title');
	my $_link = "http://www.fayland.org/journal/$cat->{'cms_file'}.html";
	my $_description = $cat->{'cms_describe'};
	my $_create_data = $cat->{'cms_cre_data'};
	# convert to the standard w3cdtf
	$_create_data = date2w3cdtf($_create_data);
	# got the modified data
	my $_modified_data = $cat->{'cms_mod_data'};
	# if not exists, use create data instead
	if ($_modified_data) {
		$_modified_data = date2w3cdtf($_modified_data);
	} else {
		$_modified_data = $_create_data;
	}
		
	$atom->add_entry(
		title    => $_title,
		link     => $_link,
		author   => { name => "Fayland Lam" },
		issued   => $_create_data,
		created  => $_create_data,
		modified => $_modified_data,
		content  => $_description,
	)
	or die;
}

$atom->save_file($Atom_file);

sub date2w3cdtf {
	my $data = shift;
	# the original data foramt like 2005-03-29 23:02:14 & it's a localtime
	# so we convert localtime to $time and got the gmtime

	my ($year, $mon, $mday, $hour, $min, $sec) = ($data =~ /^(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)$/);
	$mon--;
	use Time::Local;
	my $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
	( $sec, $min, $hour, $mday, $mon, $year) = gmtime($time);
	$year += 1900; $mon++;

	# at last we return the w3c dtf
	my $timestring = sprintf( "%4d-%02d-%02dT%02d:%02d:%02dZ",
        $year, $mon, $mday, $hour, $min, $sec );
 	return ($timestring);
}
</code></pre></div>
<p><&lt;Previous: <a href="ShareURL0503.html">Share the URLs with u (Mar. 2005)</a>&nbsp;&nbsp;>>Next: <a href="050330.html">Class::DBI 改 Primary 项</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BA%20Atom&url=http://www.fayland.org/journal/Atom.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='ShareURL0503.html'>Share the URLs with u (Mar. 2005)</a> < <span class='digit'>2005-03-08 00:36:53</span> ></li><li><a href='ShareURL0506.html'>Share the URLs (June 2005)</a> < <span class='digit'>2005-06-01 23:51:11</span> ></li><li><a href='ShareURL0504.html'>Share the URLs with u (Apr. 2005)</a> < <span class='digit'>2005-04-01 10:18:51</span> ></li><li><a href='ShareURL0505.html'>Share the URLs with u (May 2005)</a> < <span class='digit'>2005-05-10 22:16:17</span> ></li><li><a href='ShareURL0509.html'>Share the URLs (September 2005)</a> < <span class='digit'>2005-09-22 12:29:39</span> ></li><li><a href='ShareURL0510.html'>Share the URLs (October 2005)</a> < <span class='digit'>2005-10-08 10:38:10</span> ></li><li><a href='ShareURL0512.html'>Share the URLs (December 2005)</a> < <span class='digit'>2005-12-01 00:13:53</span> ></li></ul>
Created on <span class="digit">2005-03-29 00:20:42</span>, Last modified on <span class="digit">2005-03-29 23:24:58</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>